/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package csse3004.GD;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Tom McNair - S41758984
 * @author David Bullock 42062222
 */
public class Poll implements Serializable {

    protected final Log logger = LogFactory.getLog(getClass());
    private int id = -1;
    private String name;
    private boolean anonymous;
    private boolean public_result;
    private ArrayList<Question> questions = null;
    private int firstQuestionID;
    private boolean hasParticipantList;
    private boolean open;
    private Date creationDate;

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }
    
    public int getFirstQuestionID() {
        return firstQuestionID;
    }

    public void setFirstQuestionID(int firstQuestionID) {
        this.firstQuestionID = firstQuestionID;
    }

    public int getId() {
        return id;
    }

    public void setId(int ID) {
        this.id = ID;
    }

    public boolean isPublicResult() {
        return public_result;
    }

    public void setPublicResult() {
        this.public_result = true;
    }

    public void setPrivateResult() {
        this.public_result = false;
    }

    public boolean isAnonymous() {
        return anonymous;
    }

    public void setAnonymous(boolean anonymous) {
        this.anonymous = anonymous;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ArrayList<Question> getQuestions() {
        return questions;
    }

    /**
     * Sets the list of all questions and sets the parents of each of those
     * questions to reflect this poll's ID, requires the ID to be set
     * @param questions the list of questions to add
     * @return false if the ID hasn't been set, true on success.
     */
    public boolean setQuestions(ArrayList<Question> questions) {
        if (id == -1) {
            return false;
        }
        for (Question r : questions) {
            r.setPollId(id);
        }
        this.questions = questions;
        return true;
    }

    public boolean addQuestion(Question question) {
        if (id == -1) {
            return false;
        }
        question.setPollId(id);
        this.questions.add(question);
        return true;
    }

    public boolean hasParticipantList() {
        return hasParticipantList;
    }

    public void setParticipantList(boolean ParticipantList) {
        this.hasParticipantList = ParticipantList;
    }

    public boolean isOpen() {
        return open;
    }

    public void setOpen(boolean open) {
        this.open = open;
    }  
    
    public ArrayList<Poll> genTestPoll() {
        ArrayList<Poll> p = new ArrayList<Poll>();
        for (int i = 0; i < 20; i++) {
            p.add(new Poll());
            p.get(i).setId(i);
            p.get(i).setName("Poll " + i);
            if (((i % 3) == 0) || ((i % 5) == 0)) {
                p.get(i).setPrivateResult();
            } else {
                p.get(i).setPublicResult();
            }
            if ((i % 2) == 0) {
                p.get(i).setAnonymous(true);
                p.get(i).setParticipantList(false);
                p.get(i).setOpen(false);
                
            } else {
                p.get(i).setAnonymous(false);
                p.get(i).setParticipantList(true);
                p.get(i).setOpen(true);
            }
            p.get(i).setQuestions(new Question().genTestQuestion(i));
        }
        return p;
    }
}
